package com.information.manage.basic.common.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * 路径构建工具
 */
public class PathUtils {

    public static String builderPath(String... args) {
        String separator = "/";
        StringBuilder stringBuilder = new StringBuilder();
        for(int i = 0; i < args.length; ++i) {
            String backPart = args[i];
            if (i == 0) {
                stringBuilder.append(backPart);
            } else if (!StringUtils.isEmpty(backPart)) {
                String frontPart = stringBuilder.toString();
                if (frontPart.endsWith(separator) && !backPart.startsWith(separator)) {
                    stringBuilder.append(backPart);
                } else if (frontPart.endsWith(separator) && backPart.startsWith(separator)) {
                    stringBuilder.append(backPart.substring(1));
                }

                if (!frontPart.endsWith(separator) && !backPart.startsWith(separator)) {
                    stringBuilder.append(separator).append(backPart);
                } else if (!frontPart.endsWith(separator) && backPart.startsWith(separator)) {
                    stringBuilder.append(backPart);
                }
            }
        }
        return stringBuilder.toString();
    }

    public static String builderPathEndSlash(String... args) {
        String separator = "/";
        StringBuilder stringBuilder = new StringBuilder(builderPath(args));
        if (!stringBuilder.toString().endsWith(separator)) {
            stringBuilder.append(separator);
        }
        return stringBuilder.toString();
    }

}
